package com.kvest.multiframe_test.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.kvest.multiframe_test.R;

/**
 * Created with IntelliJ IDEA.
 * User: r.belokon
 * Date: 22.03.13
 * Time: 14:36
 * To change this template use File | Settings | File Templates.
 */
public class HeadersFragment extends Fragment implements AdapterView.OnItemClickListener {
    private static final String LOG_TAG = "HeadersFragment";
    private ListView listView;
    private OnItemSelectedListener listener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.headers_layout , null);

        listView = (ListView) view.findViewById(R.id.headers_list_view);
        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            listener = (OnItemSelectedListener) activity;
        } catch (ClassCastException classCastException) {
            Log.d(LOG_TAG, "Activity should implements HeadersFragment.OnItemSelectedListener");
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1,
                                                                getActivity().getResources().getStringArray(R.array.headers));
        listView.setOnItemClickListener(this);
        listView.setAdapter(adapter);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if (listener != null) {
            listener.onItemSelected(i);
        }
    }

    public interface OnItemSelectedListener {
        public void onItemSelected(int id);
    }
}
